import React from 'react';
import { dictionaryList } from '@/services/dictionary';

// 获取职位列表的特殊字典
const useDictList = query => {
    const [data, setData] = React.useState([]);
    const [childData, setChildData] = React.useState([]);
    const [loading, setLoading] = React.useState(false);

    const fetchData = async (param = query) => {
        setLoading(true);

        const { data: datalist, code } = (await dictionaryList(param)) || {};

        if (code === 10000) {
            if (param.type === 'child') {
                setChildData(datalist);
            } else {
                setData(datalist);
                setChildData([]);
            }
        }

        setLoading(false);
        return datalist;
    };

    React.useEffect(() => {
        if (query.code) {
            fetchData();
        }
    }, [query.code]);

    return [loading, data, childData, fetchData];
};

export default useDictList;
